{% extends 'base.html' %}
{% load static %}

{% block title %}用户动态 - 本地有约{% endblock %}

{% block content %}
<div class="container py-4">
    <div class="row">
        <div class="col-lg-8 mx-auto">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h4 class="mb-0"><i class="fas fa-rss me-2"></i>用户动态</h4>
                    <div class="dropdown">
                        <button class="btn btn-outline-secondary btn-sm dropdown-toggle" data-bs-toggle="dropdown">
                            {% if request.user.is_authenticated %}关注的人{% else %}全部动态{% endif %}
                        </button>
                        <ul class="dropdown-menu">
                            <li><a class="dropdown-item" href="?filter=all">全部动态</a></li>
                            {% if user.is_authenticated %}
                            <li><a class="dropdown-item" href="?filter=following">关注的人</a></li>
                            {% endif %}
                        </ul>
                    </div>
                </div>
                <div class="card-body">
                    {% for activity in activities %}
                    <div class="activity-item mb-4 pb-4 border-bottom">
                        <div class="d-flex align-items-start">
                            <img src="{{ activity.user.avatar.url|default:'/static/images/default-avatar.jpg' }}"
                                 class="rounded-circle me-3" width="48" height="48" alt="头像">
                            <div class="flex-grow-1">
                                <div class="d-flex justify-content-between align-items-start mb-2">
                                    <div>
                                        <a href="{% url 'location:user_profile' activity.user.id %}" class="text-decoration-none">
                                            <strong class="text-dark">{{ activity.user.username }}</strong>
                                        </a>
                                        <span class="text-muted ms-2">{{ activity.get_activity_type_display }}</span>
                                    </div>
                                    <small class="text-muted">{{ activity.created_at|timesince }}前</small>
                                </div>

                                {% if activity.target_event %}
                                <div class="activity-content bg-light rounded p-3 mb-2">
                                    <h6 class="mb-2">
                                        <a href="{% url 'location:activity_detail' activity.target_event.id %}" class="text-decoration-none">
                                            {{ activity.target_event.title }}
                                        </a>
                                    </h6>
                                    <div class="text-muted small">
                                        <span class="me-3"><i class="fas fa-map-marker-alt me-1"></i>{{ activity.target_event.location }}</span>
                                        <span><i class="fas fa-clock me-1"></i>{{ activity.target_event.start_time|date:"m月d日 H:i" }}</span>
                                    </div>
                                </div>
                                {% endif %}

                                {% if activity.target_post %}
                                <div class="activity-content bg-light rounded p-3 mb-2">
                                    <h6 class="mb-2">
                                        <a href="{% url 'location:post_detail' activity.target_post.id %}" class="text-decoration-none">
                                            {{ activity.target_post.title }}
                                        </a>
                                    </h6>
                                    <p class="text-muted small mb-0">
                                        {{ activity.target_post.content|striptags|truncatechars:100 }}
                                    </p>
                                </div>
                                {% endif %}

                                {% if activity.target_comment %}
                                <div class="activity-content bg-light rounded p-3">
                                    <p class="mb-2">
                                        {{ activity.target_comment.content|truncatechars:150 }}
                                    </p>
                                    <small class="text-muted">
                                        回复了
                                        <a href="{% url 'location:post_detail' activity.target_comment.post.id %}" class="text-decoration-none">
                                            {{ activity.target_comment.post.title }}
                                        </a>
                                    </small>
                                </div>
                                {% endif %}

                                <div class="mt-2">
                                    {% if activity.activity_type == 'event_created' %}
                                    <a href="{% url 'location:activity_detail' activity.target_event.id %}" class="btn btn-sm btn-outline-primary">
                                        查看活动
                                    </a>
                                    {% elif activity.activity_type == 'post_created' %}
                                    <a href="{% url 'location:post_detail' activity.target_post.id %}" class="btn btn-sm btn-outline-primary">
                                        查看帖子
                                    </a>
                                    {% elif activity.activity_type == 'comment_created' %}
                                    <a href="{% url 'location:post_detail' activity.target_comment.post.id %}" class="btn btn-sm btn-outline-primary">
                                        查看评论
                                    </a>
                                    {% endif %}

                                    {% if user.is_authenticated and user != activity.user %}
                                    <button class="btn btn-sm btn-outline-secondary ms-2" onclick="toggleFollow({{ activity.user.id }})">
                                        取消关注
                                    </button>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </div>
                    {% empty %}
                    <div class="text-center py-5">
                        <i class="fas fa-stream fa-2x text-muted mb-3"></i>
                        <h5 class="text-muted">暂无动态</h5>
                        <p class="text-muted">
                            {% if request.user.is_authenticated %}
                            关注其他用户以查看他们的动态
                            {% else %}
                            登录后查看关注用户的动态
                            {% endif %}
                        </p>
                        <a href="{% url 'location:community_home' %}" class="btn btn-primary">
                            <i class="fas fa-users me-2"></i>去社区看看
                        </a>
                    </div>
                    {% endfor %}

                    <!-- 分页 -->
                    {% if page_obj.has_other_pages %}
                    <nav aria-label="动态分页" class="mt-4">
                        <ul class="pagination justify-content-center">
                            {% if page_obj.has_previous %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.previous_page_number }}">
                                    <i class="fas fa-chevron-left"></i>
                                </a>
                            </li>
                            {% endif %}

                            {% for num in page_obj.paginator.page_range %}
                                {% if page_obj.number == num %}
                                <li class="page-item active">
                                    <span class="page-link">{{ num }}</span>
                                </li>
                                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                                </li>
                                {% endif %}
                            {% endfor %}

                            {% if page_obj.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.next_page_number }}">
                                    <i class="fas fa-chevron-right"></i>
                                </a>
                            </li>
                            {% endif %}
                        </ul>
                    </nav>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>

<script>
async function toggleFollow(userId) {
    try {
        const response = await fetch(`/location/social/follow/${userId}/`, {
            method: 'POST',
            headers: {
                'X-CSRFToken': getCookie('csrftoken'),
                'X-Requested-With': 'XMLHttpRequest'
            }
        });

        const result = await response.json();

        if (result.status === 'success') {
            location.reload(); // 简单重新加载
        }
    } catch (error) {
        console.error('Error:', error);
    }
}

function getCookie(name) {
    let cookieValue = null;
    if (document.cookie && document.cookie !== '') {
        const cookies = document.cookie.split(';');
        for (let i = 0; i < cookies.length; i++) {
            const cookie = cookies[i].trim();
            if (cookie.substring(0, name.length + 1) === (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}
</script>

<style>
.activity-item:last-child {
    border-bottom: none !important;
    margin-bottom: 0 !important;
    padding-bottom: 0 !important;
}

.activity-content {
    border-left: 4px solid #0d6efd;
}
</style>
{% endblock %}